//
//  XZHSearchIllustrationViewController.m
//  Piece
//
//  Created by dllo on 16/6/11.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "XZHSearchIllustrationViewController.h"
#import "ZHNetworkHandler.h"
#import "XZHIllustrationModel.h"
#import "XZHIllustrationTableViewCell.h"
#import "XZHReadTableViewCell.h"
#import "XZHMusicTableViewCell.h"
#import "XZHMovieTableViewCell.h"
#import "DYJShortViewController.h"
#import "DYJSerialViewController.h"
#import "DYJQuestionViewController.h"
#import "XZHHomeDetailViewController.h"
#import "XZHMovieDetailViewController.h"
#import "XZHMovieDetailModel.h"
#import "DXYMusicViewController.h"
#import "YXJSearchModel.h"
#import "YXJPushMusicViewController.h"

@interface XZHSearchIllustrationViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation XZHSearchIllustrationViewController {
    UIImageView *placeholderImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpTableView];
    [self getDataByUrl];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.86 green:0.87 blue:0.88 alpha:1.00];
    placeholderImageView = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth - 300) / 2, 120, 300, 200)];
    placeholderImageView.image = [UIImage imageNamed:@"search_null_image"];
    placeholderImageView.hidden = YES;
    [self.view addSubview:placeholderImageView];
    [self.view bringSubviewToFront:placeholderImageView];
    [placeholderImageView setTag:1230];
    
    // Do any additional setup after loading the view.
}

- (void)setUpTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 49 - 35 - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"XZHIllustrationTableViewCell" bundle:nil] forCellReuseIdentifier:@"illustrationCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"XZHReadTableViewCell" bundle:nil] forCellReuseIdentifier:@"readCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"XZHMusicTableViewCell" bundle:nil] forCellReuseIdentifier:@"musicCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"XZHMovieTableViewCell" bundle:nil] forCellReuseIdentifier:@"movieCell"];
    
   
}

- (void)getDataByUrl {
    NSString *url = [NSString string];
    if (_curIndex == 0) {
        url = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/search/hp/%@", _key];
    } else if (_curIndex == 1){
        url = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/search/reading/%@", _key];
    } else if (_curIndex == 2){
        url = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/search/music/%@", _key];
    } else {
        url = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/search/movie/%@", _key];
    }
    [ZHNetworkHandler getDataByURLString:url WithBodyString:nil WithHeaderDic:nil WithResponse:0 WithSuccess:^(id result) {
        self.dataSource = [NSMutableArray array];
        for (NSDictionary *dic in [result objectForKey:@"data"]) {
            XZHIllustrationModel *model = [[XZHIllustrationModel alloc] initWithDic:dic];
            [self.dataSource addObject:model];
        }
        if (_dataSource.count == 0) {
            placeholderImageView.hidden = NO;
            _tableView.hidden = YES;
        }
        else
        {
            placeholderImageView.hidden = YES;
            _tableView.hidden = NO;
        }

        [self.tableView reloadData];
    } WithFailure:^(NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataSource.count == 0) {
        
    } else {
        [self.view viewWithTag:1230].hidden = YES;
    }
    if (_curIndex == 0) {
        XZHIllustrationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"illustrationCell"];
        cell.strKey = _key;
        cell.model = [self.dataSource objectAtIndex:indexPath.row];
        return cell;
    } else if (_curIndex == 1){
        XZHReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"readCell"];
        cell.strKey = _key;
        cell.model = [self.dataSource objectAtIndex:indexPath.row];
        return cell;
    } else if (_curIndex == 2){
        XZHMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"musicCell"];
        cell.strKey = _key;
        cell.model = [self.dataSource objectAtIndex:indexPath.row];
        return cell;
    } else {
        XZHMovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieCell"];
        cell.strKey = _key;
        cell.model = [self.dataSource objectAtIndex:indexPath.row];
        return cell;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (_curIndex == 0) {
        XZHHomeDetailViewController *dvc = [[XZHHomeDetailViewController alloc] init];
        dvc.model = [self.dataSource objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:dvc animated:YES];
    } else if (_curIndex == 1) {
        XZHIllustrationModel *model = [self.dataSource objectAtIndex:indexPath.row];
        if ([model.type isEqualToString:@"essay"]) {
            DYJShortViewController *svc = [[DYJShortViewController alloc] init];
            svc.shortId = model.id;
            [self.navigationController pushViewController:svc animated:YES];
        }
        else if ([model.type isEqualToString:@"serial"])
        {
            DYJSerialViewController *sevc = [[DYJSerialViewController alloc] init];
            sevc.serialId = model.id;
            [self.navigationController pushViewController:sevc animated:YES];
        }
        else if ([model.type isEqualToString:@"question"])
        {
            DYJQuestionViewController *qvc = [[DYJQuestionViewController alloc] init];
            qvc.questionId = model.id;
            [self.navigationController pushViewController:qvc animated:YES];
        }
    } else if (_curIndex ==2) {
        YXJPushMusicViewController *musicVC = [[YXJPushMusicViewController alloc] init];
//        YXJSearchModel *model = [self.dataSource objectAtIndex:indexPath.row];
        XZHIllustrationModel *model = [self.dataSource objectAtIndex:indexPath.row];
        musicVC.pageSSSS = [model.id integerValue];
        [self.navigationController pushViewController:musicVC animated:YES];
        
    } else {
        XZHMovieDetailViewController *mvc = [[XZHMovieDetailViewController alloc] init];
        XZHIllustrationModel *model = [self.dataSource objectAtIndex:indexPath.row];
        mvc.movie_id = model.id;
        [self.navigationController pushViewController:mvc animated:YES];
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
